#!/bin/sh  

alias curl="curl -stderr"

#listquote.sh <list> <anything-to-indicate-premarket>

if [ $# -ne 1 ]; then
	echo -e "Ticker \t Quote \t  premarket%  \t Industry" 
else
	echo "Ticker Quote Volume% change% Industry% Industry" 
fi
for stock in `cat $1 |grep -v "#"| cut -d',' -f1|awk '{print $1}'`
do
    	export YahooQuote=`curl "http://download.finance.yahoo.com/d/quotes.csv?s=$stock&f=snpol1p2wmryva2cn"`
	export PreMarket=`curl "http://finance.yahoo.com/q?s=$stock" |grep "as of"  |egrep  -o 'up|down|\([0-9]+.[0-9]+%\)' |head -n 2 |tr '\n' ' '|sed -e 's/down/-/g' -e 's/up/+/g' -e 's/ //g' -e 's/(//g' -e 's/)//g'`

    	export RealTime=`echo $YahooQuote|cut -d, -f5 | sed 's/N\/A//g'|awk '{printf"%2.2f",$1}'`
    	export percent=`echo $YahooQuote|cut -d, -f13 | sed -e 's/N\/A//g' -e 's/"//g'|awk '{print $3}'`
    	export Volume=`echo $YahooQuote|cut -d, -f11 | sed 's/N\/A//g'`
    	export AvgVolume=`echo $YahooQuote|cut -d, -f12 | sed 's/N\/A//g'`
	if [ "$Volume" != "" ] 
	then
    		export VolDiff=`echo "scale=2; $Volume / $AvgVolume"|bc `
 	fi	
    	export industryquery=`curl  "http://finance.yahoo.com/q/in?s=$stock" |sed 's/\<tr\>/\n/g' |egrep "\^YHO[a-zA-Z][0-9]{1,4}"`
    	export industry=`echo $industryquery |egrep -o ">[A-Z][a-z].*[A-Za-z]<"|sed -e 's/<//g' -e 's/>//g' -e 's/,//g' -e 's/ /_/g' -e 's/\&amp;//g'`
	export industryupdown=`echo $industryquery |egrep  -o "Down|Up" |sed 's/Down/-/g' |sed 's/Up/+/g'`
	export industrypercent=`echo $industryquery |egrep -o ">[0-9]+.[0-9]+%" |sed 's/>//g'`


	if [ $# -ne 1 ]; then
		echo -e "$stock \t $RealTime \t $PreMarket \t  $industry" 
	else
		echo "$stock $RealTime $VolDiff $percent $industryupdown$industrypercent $industry" | \
		awk '{printf("%5s%7.2f%5.2f%10s%11s|%-20s\n"),$1,$2,$3,$4,$5,$6}'
	fi
done
